package com.yuyou.fn.activity.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.yuyou.fn.activity.entity.Activity;
import com.yuyou.fn.activity.entity.ActivityCampus;
import com.yuyou.fn.activity.service.IActivityCampusInventoryService;
import com.yuyou.fn.activity.service.IActivityCampusService;
import com.yuyou.fn.activity.service.IActivityService;
import com.yuyou.fn.activity.vo.ActivityInventoryQueryVo;
import com.yuyou.fn.activity.vo.ActivityInventorySaveOrUpdateVo;
import com.yuyou.fn.activity.vo.ActivityInventorySettingDetailVo;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.Campus;
import com.yuyou.fn.educational.entity.Period;
import com.yuyou.fn.educational.service.ICampusService;
import com.yuyou.fn.educational.service.IPeriodService;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.service.IAreaTeamService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import com.yuyou.fn.activity.mapper.ActivityInventorySettingMapper;
import com.yuyou.fn.activity.entity.ActivityInventorySetting;
import com.yuyou.fn.activity.service.IActivityInventorySettingService;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.awt.geom.Area;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 * ActivityInventorySetting 表数据服务层接口实现类
 *
 */
@Service
public class ActivityInventorySettingServiceImpl extends SuperServiceImpl<ActivityInventorySettingMapper, ActivityInventorySetting,Long> implements IActivityInventorySettingService {

    @Resource
    private IAreaTeamService areaTeamService;

    @Resource
    private IActivityCampusService activityCampusService;

    @Resource
    private IActivityService activityService;

    @Resource
    private ICampusService campusService;

    @Resource
    private IActivityCampusInventoryService activityCampusInventoryService;

    @Resource
    private IPeriodService periodService;


    @Override
    @Transactional
    public void saveOrUpdateActivityInventorySetting(ActivityInventorySaveOrUpdateVo activityInventorySaveOrUpdateVo) {
        Date now= TimeUtils.now();
        if (activityInventorySaveOrUpdateVo.getActivityId()==null && StringUtils.isEmpty(activityInventorySaveOrUpdateVo.getPeriodIds())){
            throw new BusException("请选择活动或者期数");
        }
        ActivityInventorySetting activityInventorySetting=new ActivityInventorySetting();
        BeanUtils.copyProperties(activityInventorySetting,activityInventorySaveOrUpdateVo);
        if (StringUtils.isNotEmpty(activityInventorySaveOrUpdateVo.getPeriodIds())){
            List<Period> periodList=periodService.findByIds(Utils.toLongIds(activityInventorySaveOrUpdateVo.getPeriodIds()));
            activityInventorySetting.setPeriodNames(StringUtils.join(BeanUtils.toList(periodList,"periodName")));
        }
        activityInventorySetting.setUpdateTime(now);
        if (activityInventorySetting.getActivityInventorySettingId()==null){
            activityInventorySetting.setCreateTime(now);
            this.insertSelective(activityInventorySetting);
        }else {
            this.updateSelectiveById(activityInventorySetting);
        }
        if (activityInventorySaveOrUpdateVo.getActivityInventorySettingId()==null){
            List<Long> campusIds=null;
            if (activityInventorySaveOrUpdateVo.getActivityId()!=null){
                List<ActivityCampus> activityCampusList = activityCampusService.findActivityCampusList(activityInventorySaveOrUpdateVo.getActivityId());
                campusIds = BeanUtils.toList(activityCampusList, "campusId");
                if (Utils.isEmpty(campusIds)){
                    throw new BusException("选择的活动还未关联校区");
                }
            }else {
                List<Campus> campusList=campusService.findCampusByAreaTeamIdState(activityInventorySaveOrUpdateVo.getAreaTeamId(),1);
                campusIds = BeanUtils.toList(campusList, "campusId");
            }
            if (campusIds.size()>0){
                activityCampusInventoryService.saveActivityCampusInventory(activityInventorySetting,campusIds);
            }
        }else {
            activityCampusInventoryService.updateByActivityInventorySettingId(activityInventorySaveOrUpdateVo.getActivityInventorySettingId(),activityInventorySetting);
        }
    }

    @Override
    public Page<ActivityInventorySetting> findActivityInventoryPage(ActivityInventoryQueryVo activityInventoryQueryVo, int pageNo, int pageSize) {
        Example<ActivityInventorySetting> example=ActivityInventorySetting.createExample();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,activityInventoryQueryVo);
        example.orderBy("createTime").desc();
        return this.findPageByExample(example,pageNo,pageSize);
    }

    @Override
    public Page<ActivityInventorySettingDetailVo> findActivityInventoryDetailPage(ActivityInventoryQueryVo activityInventoryQueryVo, int pageNo, int pageSize) {
        AreaTeam areaTeam=areaTeamService.findById(activityInventoryQueryVo.getAreaTeamId());
        Page<ActivityInventorySetting> activityInventoryPage = this.findActivityInventoryPage(activityInventoryQueryVo, pageNo, pageSize);
        Page<ActivityInventorySettingDetailVo> activityInventorySettingDetailVoPage= Utils.newPage(activityInventoryPage);
        List<ActivityInventorySetting> activityInventorySettingList=activityInventoryPage.getRecords();
        if (activityInventorySettingList!=null){
            List<Long> activityIds=BeanUtils.toList(activityInventorySettingList,"activityId");
            List<Activity> activityList=activityService.findByIds(activityIds);
            Map<Long, Activity> activityMap = BeanUtils.toMap(activityList, "activityId");
            List<ActivityInventorySettingDetailVo> activityInventorySettingDetailVoList = Lists.transform(activityInventorySettingList, item -> {
                ActivityInventorySettingDetailVo activityInventorySettingDetailVo = new ActivityInventorySettingDetailVo();
                BeanUtils.copyProperties(activityInventorySettingDetailVo, item);
                activityInventorySettingDetailVo.setAreaTeamName(areaTeam.getName());
                if(item.getActivityId()!=null){
                    activityInventorySettingDetailVo.setActivityName(activityMap.get(item.getActivityId()).getActivityName());
                }
                return activityInventorySettingDetailVo;
            });
            activityInventorySettingDetailVoPage.setRecords(activityInventorySettingDetailVoList);
        }
        return activityInventorySettingDetailVoPage;
    }

    @Override
    public ActivityInventorySettingDetailVo findActivityInventoryDetailById(long activityInventorySettingId) {
        ActivityInventorySetting inventorySetting = this.findById(activityInventorySettingId);
        AreaTeam areaTeam=areaTeamService.findById(inventorySetting.getAreaTeamId());
        ActivityInventorySettingDetailVo activityInventorySettingDetailVo=new ActivityInventorySettingDetailVo();
        BeanUtils.copyProperties(activityInventorySettingDetailVo,inventorySetting);
        activityInventorySettingDetailVo.setAreaTeamName(areaTeam.getName());
        return activityInventorySettingDetailVo;
    }
}